{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e98598b3",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'torch' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_15968\\1518444399.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mmyFinrtuneModel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mModule\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0membedding_dim\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhidden_dim\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m         \u001b[0msuper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmyFinrtuneModel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbert\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbert\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlstm\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLSTM\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0membedding_dim\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mhidden_size\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mhidden_dim\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum_layers\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdropout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mbidirectional\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mbatch_first\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'torch' is not defined"
     ]
    }
   ],
   "source": [
    "class myFinetuneModel(torch.nn.Module):\n",
    "    def __init__(self, embedding_dim, hidden_dim):\n",
    "        super(myFinetuneModel,self).__init__()\n",
    "        self.bert = bert\n",
    "        self.lstm = nn.LSTM(embedding_dim,hidden_size=hidden_dim, num_layers = 1,dropout=0.5,bidirectional = True,batch_first = True)\n",
    "        self.hidden = nn.Linear(hidden_dim*2,hidden_dim)\n",
    "        self.hidden1 = nn.Linear(hidden_dim+1,tag_intent_num)\n",
    "        self.relu = nn.LeakyReLU()\n",
    "        self.crf = CRF(tag_intent_num)\n",
    "    def forward(self,batch_input_ids,labels,batch_speaker,mask=None):\n",
    "        outputs = self.bert(batch_input_ids)['pooler_output'].unsqueeze(0)\n",
    "        lstm_out,self.hiddenn = self.lstm(outputs)\n",
    "        hidden1 = self.hidden(lstm_out)\n",
    "        hidden1 = torch.cat((hidden1,batch_speaker),2)\n",
    "        relu = self.relu(hidden1)\n",
    "        lstm_feats = self.hidden1(relu)\n",
    "        if labels is not None: #训练用   #mask=attention_masks.byte()\n",
    "            if mask is not None:\n",
    "                loss = -1.*self.crf(emissions=lstm_feats,tags=labels,mask=mask.permute(1,0),reduction='mean')   #outputs=(batch_size,)   输出log形式的likelihood\n",
    "            else:\n",
    "                loss = -1.*self.crf(emissions=lstm_feats,tags=labels,reduction='mean')\n",
    "            return loss\n",
    "        else:   #测试用\n",
    "            if mask is not None:\n",
    "                prediction = self.crf.decode(emissions=lstm_feats,mask=mask.permute(1,0))   #mask=attention_masks.byte()\n",
    "            else:\n",
    "                prediction = self.crf.decode(emissions=lstm_feats)\n",
    "            return prediction\n",
    "        return out\n",
    "    def getlstm_out(self,batch_input_ids):\n",
    "        outputs = self.bert(batch_input_ids)['pooler_output'].unsqueeze(0).detach()\n",
    "        lstm_out,self.hiddenn = self.lstm(outputs)\n",
    "        lstm_out1 = lstm_out.detach()\n",
    "        return lstm_out\n",
    "    def getbert_out(self,batch_input_ids):\n",
    "        outputs = self.bert(batch_input_ids)['pooler_output'].unsqueeze(0).detach()\n",
    "        return outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "016e02f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bert1",
   "language": "python",
   "name": "bert1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
